// 导出承运单报表
/**Copyright 2016 zhaojian */

interface IExportReportCarrierViewModel extends IBaseViewModel<IExportReportQueryTerms>{
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<reportCarrierOrderListResponse>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";

    /**
     * 导出报表
     */
    excel: ()=> void;
    /**
     * 导出报表2
     */
    excel2: ()=> void;
    
    /**
     * 查询
     */
    query:()=>void;
     /**
     * 列表高度
     */
    heightA:number;

}

/**查询条件 */
interface IExportReportQueryTerms{
    /**
     * 合同编号
     */
    contractNum:string;
    /**
     * 客户单位 
     */
    clientName:string;
    /**
     * 发货地址 
     */
    shipAddress:string;
    /**
     * 送货地址 
     */
    deliverAddress:string;
    /**
     * 发货起始时间 
     */
    shipStartTime:string;
    /**
     * 发货结束时间 
     */
    shipEndTime:string;
    /**
     * 承运商 
     */
    carrierName:string;
    /**
     * 驾驶员 
     */
    driver:string;


}


class IExportController extends ListController<IExportReportCarrierViewModel,IExportReportQueryTerms> {
    gridApi: uiGrid.IGridOptionsOf<reportCarrierOrderListResponse>;
    /**
     * 排序字段枚举
     */
    orderBy: number = 0;
    /**
     * 是否升序
     */
    asc: boolean = true;
    /**
     * 翻页
     */
    skip:number = 0;
    
    constructor($scope:IExportReportCarrierViewModel, private orderService:IorderService, private valueService: IValueService,
    private $ngBootbox: BootboxService, private $state: angular.ui.IStateService,private routeService:routeService,private $location:ng.ILocationService,private exportService:IexportService){
        super($scope);
        this.init();
    }

    /**
     * 初始化
     */
     init():void{
        super.init();
        
        /**查询初始化 */
        this.$scope.queryTerms = {
            contractNum:"",
            clientName: "",
            shipAddress: "",
            deliverAddress: "",
            shipStartTime: "",
            shipEndTime: "",
            carrierName: "",
            driver: "",
           
        };

           this.$scope.gridOptions = {
            enableRowSelection: true,
            enableRowHeaderSelection: false,
            enableSelectAll: false,//全选禁用
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
               { displayName: '客户单位', field: 'clientName', cellTemplate: '<div ng-repeat="item in row.entity.orderList" class="LTL-list" title="{{item.clientName}}">{{item.clientName}}</div>', enableColumnMenu: false, headerTooltip: true, width: '9%' },
                        { displayName: '起始地', field: 'originAddress', cellTemplate: '<div ng-repeat="item in row.entity.orderList" class="LTL-list" title="{{item.originAddress}}">{{item.originAddress}}</div>', enableColumnMenu: false, headerTooltip: true, width: '9%' },
                        { displayName: '目的地', field: 'destinationAddress', cellTemplate: '<div ng-repeat="item in row.entity.orderList" class="LTL-list" title="{{item.destinationAddress}}">{{item.destinationAddress}}</div>', enableColumnMenu: false, headerTooltip: true, width: '9%' },
                        { displayName: '发货时间', field: 'deliveryTime', cellTemplate: '<div ng-repeat="item in row.entity.orderList" class="LTL-list" title="{{item.deliveryTime}}">{{item.deliveryTime}}</div>', enableColumnMenu: false, headerTooltip: true, width: '6%' },
                        { displayName: '货物名称', field: 'goodsName', cellTemplate: '<div ng-repeat="item in row.entity.orderList" class="LTL-list" title="{{item.goodsName}}">{{item.goodsName}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '货物数量', field: 'originAddress', cellTemplate: '<div ng-repeat="item in row.entity.orderList" class="LTL-list" title="{{item.quantityOfGoods}}">{{item.quantityOfGoods}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '应收总价', field: 'receivableSum', cellTemplate: '<div ng-repeat="item in row.entity.orderList" class="LTL-list" title="{{item.receivableSum}}">{{item.receivableSum}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '调度员', field: 'dispatcher', cellTemplate: '<div ng-repeat="item in row.entity.orderList" class="LTL-list" title="{{item.dispatcher}}">{{item.dispatcher}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },

                        { displayName: '合同号', field: 'contractNumber', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list"                        title="{{row.entity.contractNumber}}">{{row.entity.contractNumber}}</div>', enableColumnMenu: false, headerTooltip: true, },
                        { displayName: '应付单价', field: 'payablePrice', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.payablePrice}}">{{row.entity.payablePrice}}</div>', enableColumnMenu: false, headerTooltip: true, },

                        { displayName: '预付款', field: 'advancePayment', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.advancePayment}}">{{row.entity.advancePayment | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '易耗品(预付)', field: 'consumablePrePay', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.consumablePrePay}}">{{row.entity.consumablePrePay | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '易耗品(月结)', field: 'consumableMonthly', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.consumableMonthly}}">{{row.entity.consumableMonthly | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '吊费(预付)', field: 'liftingChargePrePay', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.liftingChargePrePay}}">{{row.entity.liftingChargePrePay | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '吊费(月结)', field: 'liftingChargeMonthly', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.liftingChargeMonthly}}">{{row.entity.liftingChargeMonthly | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '卸车费(预付)', field: 'unloadingChargePrePay', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.unloadingChargePrePay}}">{{row.entity.unloadingChargePrePay | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '卸车费(月结)', field: 'unloadingChargeMonthly', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.unloadingChargeMonthly}}">{{row.entity.unloadingChargeMonthly | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '保险费(预付)', field: 'premiumPrePay', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.premiumPrePay}}">{{row.entity.premiumPrePay | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '保险费(月结)', field: 'premiumMonthly', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.premiumMonthly}}">{{row.entity.premiumMonthly | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '额外费用', field: 'extraCharge', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.extraCharge}}">{{row.entity.extraCharge | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '货到打卡', field: 'deliveryToBank', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.deliveryToBank}}">{{row.entity.deliveryToBank | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '货到收款', field: 'deilveryToCash', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.deilveryToCash}}">{{row.entity.deilveryToCash | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '回单款(月结)', field: 'payReceipt', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.payReceipt}}">{{row.entity.payReceipt | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '回单款(预付)', field: 'payReceiptPrePay ', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.payReceiptPrePay}}">{{row.entity.payReceiptPrePay | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '短驳费', field: 'shortBargeCost', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.shortBargeCost}}">{{row.entity.shortBargeCost | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '进仓费', field: 'intoWarehouseCost', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.intoWarehouseCost}}">{{row.entity.intoWarehouseCost | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '其他', field: 'other', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.other}}">{{row.entity.other | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
                        { displayName: '费用合计', field: 'feeSum', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list" title="{{row.entity.feeSum}}">{{row.entity.feeSum | numbersZeroIsNULL}}</div>', enableColumnMenu: false, headerTooltip: true, enableSorting: false, },
            ],
            onRegisterApi: (gridApi) => {
                    gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                    gridApi.core.on.sortChanged(this.$scope, this.sortChange);
                   
                }
        }

        $('#opendataTime').click(function () {
         $('#dataTime').datetimepicker('show');
         });
         $('#opendataTimeEnd').click(function () {
         $('#dataTimeEnd').datetimepicker('show');
         });

         this.query();

     }

     /**
      * 存储查询条件
      */
     localHistory():void{
        super.localHistory(this.$state);
    }
    getHistory():void{
        super.getHistory(this.$state,this.$location)
    }

     /**
     * 加载数据
     */
    loadData(queryTerms:IExportReportQueryTerms,skip:number,count:number):void{
        this.orderService.reportCarrierOrderList(this.orderBy.toString(),this.asc,queryTerms.contractNum,queryTerms.clientName,queryTerms.shipAddress,queryTerms.deliverAddress,queryTerms.shipStartTime,queryTerms.shipEndTime,queryTerms.carrierName,queryTerms.driver,skip,count).then((result)=>{
                this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;
                //console.log( result.data.data.length);

                let gridHeight = 0;
                result.data.data.forEach((item,index)=>{
                    gridHeight = gridHeight + item.orderList.length;
                     
                });
               
                this.$scope.heightA = (gridHeight * 39)+96;

                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            });
    }

    /**
     * 排序事件触发
     */
    sortChange=(grid:uiGrid.IGridInstanceOf<reportCarrierOrderListResponse>, sortColumns: Array<uiGrid.IGridColumnOf<reportCarrierOrderListResponse>>)=>{
        if (sortColumns.length == 0) {
            this.reload();
        } else {
            let sortItem = sortColumns[0];
            switch (sortItem.name) {
                case "orderId": {
                    this.orderBy = 1;
                } break;
                case "carryCarCode": {
                    this.orderBy = 2;
                } break;
                case "carLength": {
                    this.orderBy = 3;
                } break;
                case "deliveryTime": {
                    this.orderBy = 4;
                } break;
                case "originAddress": {
                    this.orderBy = 5;
                } break;
                case "destinationAddress": {
                    this.orderBy = 6;
                } break;
                case "viaAddress": {
                    this.orderBy = 7;
                } break;
                case "contractNumber": {
                    this.orderBy = 8;
                } break;
                case "contractStatus": {
                    this.orderBy = 9;
                } break;
                case "oilCardStatus": {
                    this.orderBy = 10;
                } break;
                case "receiptStatus": {
                    this.orderBy = 11;
                } break;
                case "clientName": {
                    this.orderBy = 12;
                } break;
                case "orderStatus":{
                    this.orderBy = 13;
                }break;
            }
            this.asc = sortItem.sort.direction == "asc";
            this.reload();
       }
    }

     /**
     * 导出报表
     */
    exportFunc=(arg?:boolean)=>{
        if(arg){
            if(this.$scope.gridOptions.totalItems>60000){
                this.$ngBootbox.alert('导出数据不可超过6万条');
                return;
            }else{
                this.exportService.expoetReportCarrierOrderList(this.orderBy,this.asc,this.$scope.queryTerms.contractNum,this.$scope.queryTerms.clientName,this.$scope.queryTerms.shipAddress,this.$scope.queryTerms.deliverAddress,this.$scope.queryTerms.shipStartTime,this.$scope.queryTerms.shipEndTime,this.$scope.queryTerms.carrierName,this.$scope.queryTerms.driver,true);
            }
            
        }else{
            if(this.$scope.gridOptions.totalItems>60000){
                this.$ngBootbox.alert('导出数据不可超过6万条');
                return;
            }else{
                this.exportService.expoetReportCarrierOrderList(this.orderBy,this.asc,this.$scope.queryTerms.contractNum,this.$scope.queryTerms.clientName,this.$scope.queryTerms.shipAddress,this.$scope.queryTerms.deliverAddress,this.$scope.queryTerms.shipStartTime,this.$scope.queryTerms.shipEndTime,this.$scope.queryTerms.carrierName,this.$scope.queryTerms.driver,false);
            }
        }
    }
    }

angular.module("tms").controller("ExportController", ["$scope", "orderService", "valueService", "$ngBootbox",  "$state", "routeService","$location","exportService",IExportController]);

//过滤器
angular.module("tms").filter('numbersZeroIsNULL', function () {
    return function (input) {
        let number = "";
        if (input == 0) {
            return number;
        } else {
            number = input+"元";
        }
        return number;
    }
});